﻿using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Topro.Web.Controllers
{
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.StockOut;
    using Topro.Util;

    /// <summary>
    /// 出库控制器
    /// </summary>
    public class StockOutController : BaseController
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IUId _uid;

        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 出库接口
        /// </summary>
        private readonly StockOutService _stockOutService;

        /// <summary>
        /// 库存接口
        /// </summary>
        private readonly InventoryService _inventoryService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="stockOutService">stockOutService</param>
        public StockOutController(LogService logService, IUId uid, StockOutService stockOutService, InventoryService inventoryService)
        {
            _logService = logService;
            _uid = uid;
            _stockOutService = stockOutService;
            _inventoryService = inventoryService;
        }
        #endregion

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            ViewBag.StockOutStatus = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(StockOutStatus));

            return View();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<UserResponse></returns>
        public IActionResult StockOutQuery(StockOutQuery query)
        {
            query.Status = new List<int>()
            {
                (int)StockOutStatus.待出库,
                (int)StockOutStatus.出库中,
                (int)StockOutStatus.出库完成,
            };
            var list = _stockOutService.Pager(query);

            return Json(list);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<StockInQueryResponse></returns>
        public JsonResult InventoryQuery(InventoryQuery query)
        {
            query.OrderType = (int)OrderType.出库;
            query.Status = (int)InventoryStatus.待处理;

            var list = _inventoryService.StockOutQuery(query);

            return Json(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public JsonResult Save(StockOutSaveRequest stockOut)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //站台
                if (!stockOut.StationNo.HasValue)
                    throw new Exception("请选择入库站台.");
                stockOut.CreatedBy = UserId;
                response = _stockOutService.Save(stockOut);

            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public IActionResult CallOut()
        {
            ViewBag.NextId = _uid.NextId();

            return View("CallOut");
        }
    }
}
